package com.team.recruitment_management_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.recruitment_management_system.mapper.HrMapper;
import com.team.recruitment_management_system.pojo.dto.BasePageQuery;
import com.team.recruitment_management_system.pojo.dto.job.AddJobInfoDto;
import com.team.recruitment_management_system.pojo.dto.job.ChangeJobInfoDto;
import com.team.recruitment_management_system.pojo.dto.job.JobPageQuery;
import com.team.recruitment_management_system.pojo.entity.Hr;
import com.team.recruitment_management_system.pojo.entity.Job;
import com.team.recruitment_management_system.pojo.vo.job.JobInfoVo;
import com.team.recruitment_management_system.service.JobService;
import com.team.recruitment_management_system.mapper.JobMapper;
import com.team.recruitment_management_system.util.JwtHelper;
import com.team.recruitment_management_system.util.result.PageResult;
import com.team.recruitment_management_system.util.result.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 86147
* @description 针对表【job(职业表)】的数据库操作Service实现
* @createDate 2024-03-21 20:28:40
*/
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job>
    implements JobService{
    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private HrMapper hrMapper;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public Result<Void> addJobInfo(AddJobInfoDto addJobInfoDto) {
        if(!isHrExists(addJobInfoDto.getHrId())){
            return Result.build(null,500,"不存在该HR");
        }
        Job job = new Job();
        BeanUtils.copyProperties(addJobInfoDto,job);
        this.save(job);
        return Result.postOk(null);
    }

    @Override
    public Result<Void> changeJobInfo(ChangeJobInfoDto changeJobInfoDto) {
        Job job = this.getById(changeJobInfoDto.getId());
        if(job == null){
            return Result.build(null,500,"不存在该HR");
        }
        BeanUtils.copyProperties(changeJobInfoDto,job);
        this.updateById(job);
        return Result.putOk(null);
    }

    @Override
    public Result<Void> deleteJobById(int jobId) {
        Job job = this.getById(jobId);
        if(job == null){
            return Result.build(null,500,"不存在该HR");
        }
        this.removeById(jobId);
        return Result.deleteOk(null);
    }

    @Override
    public PageResult<JobInfoVo> seekerGetJobInfoPage(JobPageQuery jobPageQuery) {
        Page<Job> hrPage = new Page<>(jobPageQuery.getPageNum(), jobPageQuery.getPageSize());
        IPage<JobInfoVo> jobInfoVoIPage = jobMapper.seekerGetJobInfoPage(hrPage, jobPageQuery);
        return PageResult.success(jobInfoVoIPage);
    }

    @Override
    public PageResult<JobInfoVo> hrGetJobInfoPage(JobPageQuery jobPageQuery,String token) {
        Integer userId = jwtHelper.getUserId(token);
        Page<Job> hrPage = new Page<>(jobPageQuery.getPageNum(), jobPageQuery.getPageSize());
        IPage<JobInfoVo> jobInfoVoIPage = jobMapper.hrGetJobInfoPage(hrPage, jobPageQuery,userId);
        return PageResult.success(jobInfoVoIPage);
    }

    @Override
    public PageResult<JobInfoVo> getJobInfoByCompanyId(int companyId, BasePageQuery basePageQuery) {
        Page<Job> hrPage = new Page<>(basePageQuery.getPageNum(), basePageQuery.getPageSize());
        IPage<JobInfoVo> jobInfoVoIPage = jobMapper.getJobInfoPageByCompanyId(hrPage,companyId);
        return PageResult.success(jobInfoVoIPage);
    }

    @Override
    public PageResult<JobInfoVo> otherGetJobInfoPage(JobPageQuery jobPageQuery) {
        Page<Job> hrPage = new Page<>(jobPageQuery.getPageNum(), jobPageQuery.getPageSize());
        IPage<JobInfoVo> jobInfoVoIPage = jobMapper.otherGetJobInfoPage(hrPage, jobPageQuery);
        return PageResult.success(jobInfoVoIPage);
    }

    @Override
    public Result<JobInfoVo> getJobInfoById(int jobId,boolean isSeeker,boolean isHR) {
        Job job = this.getById(jobId);
        if(job == null){
            return Result.build(null,500,"不存在该职位");
        }
        if(isSeeker){
            JobInfoVo jobInfoVo = jobMapper.seekerGetJobById(jobId);
            new Thread(()-> jobMapper.updateViewNum(jobId)).start();
            return Result.ok(jobInfoVo);
        }
        if(isHR){
            // 获取请求头中的令牌
            String token = request.getHeader("Authorization");
            Integer userId = jwtHelper.getUserId(token);
            JobInfoVo jobInfoVo = jobMapper.hrGetJobById(jobId,userId);
            return Result.ok(jobInfoVo);
        }else{
            JobInfoVo jobInfoVo = jobMapper.getJobById(jobId);
            return Result.ok(jobInfoVo);
        }
    }

    @Override
    public Result<List<JobInfoVo>> getHotJob() {
        List<JobInfoVo> jobInfoVos = jobMapper.getHotJob();
        return Result.ok(jobInfoVos);
    }

    private boolean isHrExists(int hrId){
        LambdaQueryWrapper<Hr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Hr::getId,hrId);
        return hrMapper.exists(queryWrapper);
    }
}




